import pygame
import sys
from game_core import Tetris
from renderer import Renderer
from input_handler import InputHandler
from constants import SCREEN_WIDTH, SCREEN_HEIGHT

def main():
    # 初始化Pygame
    pygame.init()
    
    # 创建屏幕
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("俄罗斯方块")
    
    # 创建时钟对象，控制游戏帧率
    clock = pygame.time.Clock()
    
    # 创建游戏核心对象
    tetris_game = Tetris()
    
    # 创建渲染器
    renderer = Renderer(screen)
    # 建立renderer和game_core之间的连接
    tetris_game.renderer = renderer
    
    # 创建输入处理器
    input_handler = InputHandler(tetris_game)
    
    # 游戏主循环
    running = True
    while running:
        # 处理事件
        running = input_handler.handle_events()
        
        if not tetris_game.paused and not tetris_game.game_over:
            tetris_game.update()
        
        # 渲染游戏
        renderer.draw(tetris_game)
        # 暂停和游戏结束界面已经在renderer.draw()方法中处理
        
        # 更新屏幕显示
        pygame.display.flip()
        
        # 控制帧率
        clock.tick(60)
    
    # 退出游戏
    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()